package com.huanletao.spring.multdatasource;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@EnableTransactionManagement //启动事务管理配置；
@EnableAspectJAutoProxy(proxyTargetClass = true,exposeProxy = true)
@ComponentScan("com.huanletao.spring.multdatasource")
@EnableJpaRepositories(basePackages = "com.huanletao.spring.multdatasource.dao")
@Configuration
public class DataSourceConfig {

    /**
     * 配置主 数据源
     * @return
     */
    @Bean
    public DruidDataSource druidDataSourceMaster() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("root");
        druidDataSource.setUrl("jdbc:mysql:///quartzdb?useUnicode=true&characterEncoding=utf8&useSSL=false");
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return druidDataSource;
    }

    @Bean
    public DruidDataSource druidDataSourceSlave() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("root");
        druidDataSource.setUrl("jdbc:mysql:///db3?useUnicode=true&characterEncoding=utf8&useSSL=false");
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return druidDataSource;
    }

    @Bean(name = "routingDataSource")
    public RoutingDataSource routingDataSource() {
        Map<Object, Object> dataSourceMap = new HashMap<>();
        dataSourceMap.put("master", druidDataSourceMaster());
        dataSourceMap.put("slave", druidDataSourceSlave());

        RoutingDataSource routingDataSource = new RoutingDataSource();
        routingDataSource.setTargetDataSources(dataSourceMap);
        routingDataSource.setDefaultTargetDataSource(druidDataSourceMaster());
        return routingDataSource;
    }

    /**
     *
     * @param routingDataSource
     * @return
     */
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(RoutingDataSource routingDataSource){
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(routingDataSource);
        factoryBean.setPackagesToScan("com.huanletao.spring.multdatasource");
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setDatabase(Database.MYSQL);
        jpaVendorAdapter.setGenerateDdl(false);
        jpaVendorAdapter.setShowSql(true);
        jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
        factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        factoryBean.setJpaDialect(new HibernateJpaDialect());
        Properties jpaProperties = new Properties();
        factoryBean.setJpaProperties(jpaProperties);
        return factoryBean;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(RoutingDataSource dataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }
//
//    /**
//     * 配置事务
//     */
//    @Bean
//    public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
//        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
//        jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
//        return jpaTransactionManager;
//    }

    @Bean("transactionManagerMaster")
    public DataSourceTransactionManager transactionManagerMaster(DruidDataSource druidDataSourceMaster){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(druidDataSourceMaster);
        return dataSourceTransactionManager;
    }

    @Bean("transactionManagerSlave")
    public DataSourceTransactionManager transactionManagerSlave(DruidDataSource druidDataSourceSlave){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(druidDataSourceSlave);
        return dataSourceTransactionManager;
    }
}